import router from "./router";
// import store from "./store";
// import { Message } from "element-ui";
import NProgress from "nprogress"; // progress bar
import { getToken } from "utils/auth"; // get token from cookie
import getPageTitle from "utils/get-page-title";

NProgress.configure({ showSpinner: false }); // NProgress Configuration

export default function initPermissions() {
    router.beforeEach(async (to, from, next) => {
        // start progress bar
        NProgress.start();

        // set page title
        document.title = getPageTitle(to.meta.title);

        // determine whether the user has logged in
        const hasToken = getToken();
        next();
        // if (hasToken) {
        //     if (to.path === "/login") {
        //         // if is logged in, redirect to the home page
        //         next({ path: "/" });
        //         NProgress.done(); // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
        //     } else {
        //         next();
        //         NProgress.done();
        //     }
        // } else {
        //     next(`/login?redirect=${to.path}`);
        //     NProgress.done();
        // }
    });

    router.afterEach(() => {
        // finish progress bar
        NProgress.done();
    });
}
